////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//   Software       : S-IDENTIFICA                                            //
//   Layer          : SRC                                                     //
//   Package        :                                                         //
//   Component      : S100-EMB                                                //
//                                                                            //
//   File           : cpost.cxx                                               //
//   Author         : RSO ( Rafael Sotomayor Brûlé )                          //
//   EMail          : rsotomayor@savtec.cl                                    //
//   Type           : C++ Source                                              //
//   Usage          :                                                         //
//   Purpose        : Clase para envio da datos al servidor                   //
//                                                                            //
//                                                                            //
//   who when        what                                                     //
//   ~~~ ~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//   RSO 10-05-2011  Creation                                                 //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>

#include <iostream>
#include <string>
#include <curl/curl.h>


#include "mylib/errlog.h"
#include "mylib/utilconf.h"
#include "mylib/ClientSocket.h"
#include "mylib/SocketException.h"

#include "cpost.h"
#include "curlit.h"
#include "cconfig.h"
#include "cdatabase.h"
#include "myutil.h"
#include "cglobal.h"
#include "cxml.h"


extern CGlobal   *global_g;
extern MyUtil    *myUtil_g;
extern CConfig   *config_g;
extern CDataBase *database_g;


CPost::CPost() {

  

} ;

CPost::~CPost() {

}

int CPost::testServer() {
  errLog::LOGERR((errLog::INFO,"Testing Server\n"));
  int sockfd ;
  int timeout=5;
  char buffer[1024];
  string dummy ;

  map<string, string> mypost;

  mypost.insert(pair<string, string>("wsname", "testserver"));
  mypost.insert(pair<string, string>("idacceso", config_g->idmodulo_a));
  mypost.insert(pair<string, string>("idmodulo", config_g->idmodulo_a));
  mypost.insert(pair<string, string>("idmovil" , config_g->idmovil_a));

  sprintf(buffer,"%f",global_g->dataGeo_a.latitud);
  dummy = buffer; 
  mypost.insert(pair<string, string>("latitud" ,dummy));

  sprintf(buffer,"%f",  global_g->dataGeo_a.longitud);
  dummy = buffer; 
  mypost.insert(pair<string, string>("longitud",dummy));

  sprintf(buffer,"%f",global_g->dataGeo_a.altura);
  dummy = buffer; 
  mypost.insert(pair<string, string>("altura",dummy));

  sprintf(buffer,"%f", global_g->dataGeo_a.velocidad);
  dummy = buffer; 
  mypost.insert(pair<string, string>("velocidad",dummy));

  sprintf(buffer,"%f",global_g->dataGeo_a.rumbo);
  dummy = buffer; 
  mypost.insert(pair<string, string>("rumbo", dummy));


  sprintf(buffer,"%ld",time(NULL));
  dummy = buffer; 
  mypost.insert(pair<string, string>("ts", dummy));

  string urlpost ;
  string response;
  
  urlpost = config_g->serverHostName_a ;
  urlpost = urlpost + config_g->serverUrl_a ;
  
  CGlobal::tEstadoServidor estadoServidor ;

  errLog::LOGERR((errLog::INFO,"Verificando estado Servidor\n"));
  if ( (sockfd = connect_w_to(config_g->serverAddress_a.data(),config_g->serverPort_a,timeout) ) > 0  ) {  
    close(sockfd);
    errLog::LOGERR((errLog::TRACE,"Envia Transaccion Server %s\n",urlpost.data()));  
  
    myUtil_g->wpost(urlpost.data(),mypost,response);
    errLog::LOGERR((errLog::TRACE,"Response XML: %s\n",response.data()));
    string result;
    CXML *myxml;
    myxml = new CXML();
    myxml->parseResult(response,result) ;
    errLog::LOGERR((errLog::TRACE,"Result: %s\n",result.data()));
    if ( result == "OK" ) {
      estadoServidor = CGlobal::e_POSTOK ; 
    } else {
      estadoServidor = CGlobal::e_POSTFAIL ; 
    }
  } else {
    errLog::LOGERR((errLog::WARN, "NO se puede conectar a servidor %s:%d\n",
                              config_g->serverAddress_a.data(),config_g->serverPort_a));
    estadoServidor = CGlobal::e_CONNECTFAIL ; 
  }

  pthread_mutex_lock (&global_g->mutEstadoServidor_a); 
  global_g->flagEstadoServidorAnterior_a = global_g->flagEstadoServidor_a ;
  global_g->flagEstadoServidor_a         = estadoServidor; 
  pthread_mutex_unlock (&global_g->mutEstadoServidor_a); 

  return 0;

}
